package nl.weeaboo.sh.objects;

import java.awt.Rectangle;

import nl.weeaboo.game.gl.GLImage;
import nl.weeaboo.game.gl.GLManager;
import nl.weeaboo.game.input.UserInput;
import nl.weeaboo.sh.Game;
import nl.weeaboo.sh.level.Level;
import nl.weeaboo.sh.model.DamageRect;

public class Shot extends Sprite {

	private GLImage image;
	private Sprite owner;
	private DamageRect splashRects[];
	private ShotCollidable collidable;
	
	private double speed;
	private int hitRadius;
	
	public Shot(Game game, Level level, Sprite owner, GLImage image, int hitRadius) {
		super(game, level);

		this.owner = owner;
		this.image = image;
		
		splashRects    = new DamageRect[1];
		splashRects[0] = new DamageRect();
		setHitRadius(hitRadius);

		collidable = new ShotCollidable(game, this);		
	}

	//Functions
	public void advance(double forward) {
		advance(forward, 0);
	}
	public void advance(double forward, double side) {
		if (Math.abs(forward) >= 0.001) {
			x += Math.cos(Math.toRadians(rotation)) * forward;
			y -= Math.sin(Math.toRadians(rotation)) * forward;
		}
		if (Math.abs(side) >= 0.001) {
			x += Math.sin(Math.toRadians(rotation)) * side;
			y += Math.cos(Math.toRadians(rotation)) * side;
		}
	}
	
	public void update(UserInput input, float dt) {
		advance(speed);
		
		CollisionUtil.collideSprites(level, collidable);
		CollisionUtil.collideTiles(level, collidable);
	}

	protected void drawImage(GLManager glm) {
		image.draw(glm, -image.getWidth()/2, -image.getHeight()/2,
				image.getWidth(), image.getHeight());		
	}
	
	//Getters
	public Sprite getOwner() { return owner; }
	public double getSpeed() { return speed; }
	public double getHitRadius() { return hitRadius; }
	public Rectangle[]  getHitRects() {    return Sprite.noRects; }
	public DamageRect[] getDamageRects() { return Sprite.noDamageRects; }
	public DamageRect[] getSplashRects() { return splashRects; }
	
	//Setters
	public void setSpeed(double s) { speed = s; }
	public void setHitRadius(int r) {
		hitRadius = r;
		Rectangle rect = splashRects[0];
		rect.x = rect.y = -r;
		rect.width = rect.height = 2*r;
	}
	
}
